﻿using System;
using System.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using mojoPortal.Web.RS.SBM.AppCode.Lib;

namespace mojoPortal.Web.RS.SBM.Controls
{
    public partial class TaskListControl : RSBaseUserControl
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                RSUserIdentity userIdentity = GetRSUserIdentity();
                int companyid = userIdentity.RSCompanyId;
                //RSDataTableLookup dtl = new RSDataTableLookup();
                RSSBMDataLookupLoader.LoadStatusDDL(companyid, "TASK_CAT", ddlTaskStatus);
            }
        }
        
        protected void gvTaskList_RowDeleting(Object sender, GridViewDeleteEventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            if (userIdentity.RSCanWrite && userIdentity.RSCanDelete)
            {
                int companyid = userIdentity.RSCompanyId;
                int taskid = Convert.ToInt32(gvTaskList.DataKeys[e.RowIndex].Value);
                DeleteTask(companyid, taskid);
            }
            else
                ucModalActionMessageControl.ShowModal("You Do Not Have Permission To Perform This Action!", "", false);

        }


        private void DeleteTask(int companyid, int taskid)
        {
            if (companyid > 0 && taskid > 0)
            {
                SqlCommand sqlCmd = new SqlCommand();
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@taskid", SqlDbType.Int, 0, ParameterDirection.Input, taskid);
                SQLHelper.AddParamToSQLCmd(sqlCmd, "@companyid", SqlDbType.Int, 0, ParameterDirection.Input, companyid);
                SQLHelper.SetCommandType(sqlCmd, CommandType.Text, "DELETE FROM rs_tblWorkOrderTask WHERE TaskId=@taskid AND CompanyId=@companyid");
                SQLHelper.ExecuteScalarCmd(sqlCmd);
                int returnValue = Convert.ToInt32(sqlCmd.Parameters["@ReturnValue"].Value);
                Response.Redirect(Request.RawUrl);
            }
        }

        public void LoadTaskListData(int companyid, string sql_and = "")
        {
            SqlCommand myCommand;
            SqlParameter param;
            SqlConnection myConnection;
            sql_and = sql_and.Trim();

            String sqlSELECT = "SELECT t.TaskId, t.TaskName, t.Description, t.Response, t.StartDT, t.EndDT, t.LaborCost, t.DiscountPercent, t.DiscountAmount, t.WorkOrderId, t.AssignTo, t.LocationId, t.StatusId, t.CompanyId, (select s.StatusName from rs_tblStatus s where s.StatusId=t.statusid  AND s.category='TASK_CAT' AND s.CompanyId=@companyid) as Status FROM rs_tblWorkOrderTask t WHERE t.CompanyId=@CompanyId " + sql_and + " ORDER BY t.StartDT DESC";

            myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[GetRSCompanyConnectionString()].ToString());
            myCommand = new SqlCommand(sqlSELECT, myConnection);

            param = new SqlParameter();
            param.SqlDbType = SqlDbType.Int;
            param.ParameterName = "@companyid";
            param.Value = companyid;
            myCommand.Parameters.Add(param);

            SqlDataAdapter adapter = new SqlDataAdapter(myCommand);
            myConnection.Open();
            DataSet ds = new DataSet();
            adapter.Fill(ds);
            Session["DATATABLE"] = ds.Tables[0];
            gvTaskList.DataSource = ds;
            gvTaskList.DataBind();
            myConnection.Close();

        }

        private string ConvertSortDirectionToSql(SortDirection sortDirection)
        {
            string newSortDirection = String.Empty;

            switch (sortDirection)
            {
                case SortDirection.Ascending:
                    newSortDirection = "ASC";
                    break;

                case SortDirection.Descending:
                    newSortDirection = "DESC";
                    break;
            }
            return newSortDirection;
        }

        protected void gvTaskList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvTaskList.PageIndex = e.NewPageIndex;
            gvTaskList.DataBind();
        }

        protected void gvTaskList_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataTable dataTable = Session["DATATABLE"] as DataTable;
            if (dataTable != null)
            {
                DataView dataView = new DataView(dataTable);
                dataView.Sort = e.SortExpression + " " + ConvertSortDirectionToSql(e.SortDirection);

                gvTaskList.DataSource = dataView;
                gvTaskList.DataBind();
            }
        }

        protected void ddlTaskStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            RSUserIdentity userIdentity = GetRSUserIdentity();
            int companyid = userIdentity.RSCompanyId;
            string sql_and = "";
            int isel = Convert.ToInt32(ddlTaskStatus.SelectedValue);
            if (isel > 0)
                sql_and = "AND StatusId=" + isel;
            LoadTaskListData(companyid, sql_and);
        }
    }

}